﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Web.Script.Serialization;
using RBS.Common;
using RBS.Common.JsonEntity;
using RBS.Core.Model;
namespace RBS.Core.BLL
{
    //H_HotelMap
    public partial class H_HotelMap
    {
        private readonly RBS.Core.DAL.H_HotelMap dal = new RBS.Core.DAL.H_HotelMap();
        public H_HotelMap()
        { }

         
        public MapInfo AMapAdd(string mapid,Model.Hotel.H_Hotels hotels)
        {
            string result = string.Empty;
            if (mapid == "")
            {
                //往高德地图添加标注
                string param = "key=04a4a8f54a5dc56d6c33975fdccd81e2" +
                               "&tableid=547c0137e4b068ad0ea565e1" +
                               "&loctype=1" +
                               "&data={'_name':'" + hotels.H_Name + "','_location':'" + hotels.Location + "','_address':'" + hotels.Address + "','type':'酒店'," +
                               ",\"Telphone\":\"" + hotels.Telphone + "\"" +
                                 ",\"Cuisine\":\"" + hotels.Cuisine + "\"" +
                               "'hotelid':'" + hotels.H_id + "'}";

                result = Utils.HttpPost("http://yuntuapi.amap.com/datamanage/data/create", param);
            }
            else
            {
                string param = "key=04a4a8f54a5dc56d6c33975fdccd81e2" +
                               "&tableid=547c0137e4b068ad0ea565e1" +
                               "&loctype=1" +
                               "&data={\"_id\":\"" + mapid + "\",\"_name\":\"" + hotels.H_Name + "\"" +
                               ",\"_location\":\"" + hotels.Location + "\"" +
                               ",\"_address\":\"" + hotels.Address + "\"" +
                                ",\"Telphone\":\"" + hotels.Telphone + "\"" +
                                 ",\"Cuisine\":\"" + hotels.Cuisine + "\"" +
                               /*  ",\"_address\":\"" + hotels.Address + "\"" +
                                ",\"_address\":\"" + hotels.Address + "\"" +*/
                               ",\"hotelid\":\"" + hotels.H_id + "\"}";

                result = Utils.HttpPost("http://yuntuapi.amap.com/datamanage/data/update", param);
            }
            var serializer = new JavaScriptSerializer();
            var mapInfo = serializer.Deserialize<MapInfo>(result);
            if (mapInfo._id == null)
            {
                mapInfo._id = mapid;
            }
            return mapInfo;
        } 
        public MapInfo AMapAdd(string mapid, string hotelname, string hoteladdress, string location,int hotelid)
        {
            string result = string.Empty;
            if (mapid == "")
            {
                //往高德地图添加标注
                string param = "key=04a4a8f54a5dc56d6c33975fdccd81e2" +
                               "&tableid=547c0137e4b068ad0ea565e1" +
                               "&loctype=1" +
                               "&data={'_name':'" + hotelname + "','_location':'" + location + "','_address':'" + hoteladdress + "','type':'酒店','hotelid':'" + hotelid + "'}";

                  result = Utils.HttpPost("http://yuntuapi.amap.com/datamanage/data/create", param);
            }
            else
            {
                string param = "key=04a4a8f54a5dc56d6c33975fdccd81e2" +
                               "&tableid=547c0137e4b068ad0ea565e1" +
                               "&loctype=1" +
                               "&data={\"_id\":\"" + mapid + "\",\"_name\":\"" + hotelname + "\",\"_location\":\"" + location + "\",\"_address\":\"" + hoteladdress + "\",\"hotelid\":\"" + hotelid + "\"}";

                  result = Utils.HttpPost("http://yuntuapi.amap.com/datamanage/data/update", param);
            }
            var serializer = new JavaScriptSerializer();
            var mapInfo = serializer.Deserialize<MapInfo>(result);
            if (mapInfo._id == null)
            {
                mapInfo._id = mapid;
            } 
            return mapInfo;
        } 

        public void Save(Model.H_HotelMap model)
        {
            if (!dal.ExistsHotelId(model.HotelId))
            {
                dal.Add(model);
            }
            else
            {
                var newModel = dal.GetModelByHotelId(model.HotelId); 
                newModel.HotelId = model.HotelId;
                newModel.MapId = model.MapId;
                newModel.lat = model.lat;
                newModel.lng = model.lng;
                dal.Update(newModel);
            }
        }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            return dal.Exists(Id);
        }
        public bool ExistsHotelId(int Id)
        {
            return dal.ExistsHotelId(Id);
        }
        public Model.H_HotelMap GetModelByHotelId(int Id)
        {
            return dal.GetModelByHotelId(Id);
        }
      

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RBS.Core.Model.H_HotelMap model)
        {
            return dal.Add(model);

        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RBS.Core.Model.H_HotelMap model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            return dal.Delete(Id);
        }
        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return dal.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RBS.Core.Model.H_HotelMap GetModel(int Id)
        {

            return dal.GetModel(Id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public RBS.Core.Model.H_HotelMap GetModelByCache(int Id)
        {

            string CacheKey = "H_HotelMapModel-" + Id;
            object objModel = RBS.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(Id);
                    if (objModel != null)
                    {
                        int ModelCache = RBS.Common.ConfigHelper.GetConfigInt("ModelCache");
                        RBS.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (RBS.Core.Model.H_HotelMap)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<RBS.Core.Model.H_HotelMap> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<RBS.Core.Model.H_HotelMap> DataTableToList(DataTable dt)
        {
            var modelList = new List<RBS.Core.Model.H_HotelMap>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                RBS.Core.Model.H_HotelMap model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new RBS.Core.Model.H_HotelMap();
                    if (dt.Rows[n]["Id"].ToString() != "")
                    {
                        model.Id = int.Parse(dt.Rows[n]["Id"].ToString());
                    }
                    if (dt.Rows[n]["MapId"].ToString() != "")
                    {
                        model.MapId = dt.Rows[n]["MapId"].ToString();
                    }
                    if (dt.Rows[n]["HotelId"].ToString() != "")
                    {
                        model.HotelId = int.Parse(dt.Rows[n]["HotelId"].ToString());
                    }
                    model.lng = dt.Rows[n]["lng"].ToString();
                    model.lat = dt.Rows[n]["lat"].ToString(); 
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }
        #endregion
        #region  ExtensionMethod
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return dal.GetList(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }

        #endregion  ExtensionMethod

    }
}